$(document).ready(function() {
	$.ajax({
		type : 'post',
		url : context + '/system/module/menutreejson.jsp',
		data : {},
		dataType : 'json',
		async : false,
		beforeSend : function() {
			$("#logoutA").click(function() {
						$("#dialog-confirm").dialog("open");
					});

			$("#dialog-confirm").dialog({
				height : 170,
				width : 400,
				modal : true,
				closed : true,
				buttons : [{
							text : '确定',
							iconCls : "",
							handler : function() {
								document.location = context
										+ "/app/bcxdlogout.do";
							}
						}, {
							text : '取消',
							iconCls : "",
							handler : function() {
								$("#dialog-confirm").dialog("close");
							}
						}]

			});

		},
		success : function(data) {
			var count = data[0]["children"].length;
			if (data[0]["children"].length % 2 > 0) {
				count++;
			}
			var halfCount = count / 2;

			for (var i = 0; i < 2; i++) {
				var accordHtml = "<div class=\"demo_links\">";
				for (var j = 0; j < halfCount; j++) {
					var idx = i * halfCount + j;
					if (idx < data[0]["children"].length) {
						var n = data[0]["children"][idx];
						accordHtml += "<h2>" + n.mnname + "</h2><ul>";

						$(n["children"]).each(function(k, m) {
							accordHtml += "<li><a href=\"javascript:void(0);\" onclick=\"javascript:handleMenu('"
									+ m.realid
									+ "','"
									+ m.mnname
									+ "','"
									+ m.mnurl
									+ "');\">"
									+ m.mnname
									+ "</a>"
									+ (m.mnmemo != ""
											? "&nbsp;&nbsp;<span class=\"memo\">_"
													+ m.mnmemo + "</span>"
											: "") + "</li>";
						});
						accordHtml += "</ul>";
					}
				}
				accordHtml += "</div>";
				$("#container").append(accordHtml);
			}

		}
	});
});

/**
 * 处理菜单
 * 
 * @param {}
 *            id 菜单ID
 * @param {}
 *            name 菜单名称
 * @param {}
 *            url 菜单链接
 */
function handleMenu(id, name, url) {
	if ($("#dialogApp_" + id).length == 0) {
		var defaultWidth = 800;
		var defaultHeight = 600;

		$("<div>", {
					id : "dialogApp_" + id,
					title : name
				}).addClass("easyui-window")
				.append("<iframe width=\"100%\" height=\"99%\" frameborder=\"0\" src=\""
						+ context + url + "\"></iframe>").appendTo("#container");

		$("#dialogApp_" + id).window({
					width : defaultWidth,
					height : defaultHeight,
					modal : false
				});

	} else {
		$("#dialogApp_" + id).dialog("open");
	}
}